# -*- coding: utf-8 -*-
import allure
from common.page import Page
from dz_service.page_elements.el_common import common


class Common:

    @staticmethod
    def click_handle_now():
        Page.click(common, '公共页面', '立即办理')

    @staticmethod
    def click_next():
        Page.click(common, '公共页面', '下一步')

    @staticmethod
    def upload_id_card_pic(positive_pic_path, back_pic_path, head_pic_path=None):
        '''
        证件上传页
        @param positive_pic_path 身份证正面
        @param back_pic_path 身份证背面
        @param head_pic_path 身份证头像面
        '''
        Page.send_key(common, '公共页面', '上传人像面', positive_pic_path, log_info="人像面上传成功。")
        Page.check_page_loading(5)
        Page.send_key(common, '公共页面', '上传国徽面', back_pic_path, log_info="国徽面上传成功。")
        Page.check_page_loading(5)
        # 点击下一步
        Common().click_next()
        Page.check_page_loading(5)
        # 判断是否要上传头像
        if head_pic_path:
            Page.send_key(common, '公共页面', '上传头像面', head_pic_path, log_info="头像面上传成功。")
            Page.check_page_loading(5)
            Common().click_next()
        Page.check_page_loading(5)

    @staticmethod
    def update_recognize_result(address):
        '''
        ocr识别结果页
        @param address 证件地址
        '''
        Page.send_key(common, 'ocr结果识别页', '证件地址', address)
        Page.click(common, '公共页面', '确定', is_els=True, num=0)
        Page.check_page_loading(5)

    @staticmethod
    def check_guide_page(business_name, business_description):
        '''
        落地引导页检查
        @param business_name：业务名称
        @param business_description：业务描述
        '''
        # 检查业务名称
        Page.check_page_loading(5)
        assert Page.element(common, "引导页", "业务名称").text == business_name, "打开" + business_name + "页面失败。"
        Page.logger.info("打开" + business_name + "页面成功。", extra={"appname": "check_guide_page"})
        # 检查业务描述
        assert Page.element(common, "引导页",
                            "业务描述").text == business_description, "页面描述与需求不一致,期待为：" + business_description + "实际为：" \
                                                                  + Page.element(common, "引导页", "业务描述").text + "。"
        Page.logger.info("页面描述与需求一致,为：" + Page.element(common, "引导页", "业务描述").text + "。",
                         extra={"appname": "check_guide_page"})
        # 点击立即办理
        Common().click_handle_now()

    @staticmethod
    def check_access_conditions(els):
        """
        业务准入条件页检查
        @param els:页面期待的准入条件信息:el[1],el[2],el[3]
        """
        Page.check_page_loading(3)
        assert Page.element_is_exist(common, "业务准入条件页", "已满足"), '打开账户准入性检查页面失败。'
        Page.logger.info('打开账户准入性检查页面成功。', extra={"appname": "check_access_conditions"})
        # ServiceUtils.assert_element([(common, "业务准入条件页", "已满足", '打开账户准入性检查页面失败。')], is_els=True, num=[0, 1],
        #                             log='打开账户准入性检查页面成功。')
        lengh = len(els)
        # 检查个数是否一致
        assert len(Page.elements(common, "业务准入条件页", "业务条件描述")) == lengh, "页面业务准入条件个数与期待的准入条件信息个数不一致。"
        assert len(Page.elements(common, "业务准入条件页", "已满足")) == lengh, "页面\"已满足\"个数不正确。"
        # 检查业务准入条件是否与期待一致
        i = 0
        while i < lengh:
            assert Page.elements(common, "业务准入条件页", "业务条件描述")[i].text == els[i], "页面文字：" + els[i] + "检验失败。"
            Page.logger.info("页面文字：" + els[i] + "检验成功。", extra={"appname": "check_access_conditions"})
            i = i + 1
        # 点击下一步
        Common().click_next()

    @staticmethod
    @allure.step('step:提交结果页检查')
    def check_result_page(success_info):
        '''
        业务提交结果页检查
        @param success_info 结果页提交成功信息
        '''
        # 检查是否提示提交成功
        Page.check_page_loading(5)
        assert "提交成功" in Page.element(common, "业务提交结果页", "结果信息").text, "业务提交失败"
        assert success_info in Page.element(common, "业务提交结果页", "结果信息").text, "业务提交成功信息校验失败"
        Page.logger.info('提交成功。', extra={"appname": "check_result_page"})

    @staticmethod
    def check_dialog():
        # 若弹出一人多户同步弹窗提示，则点击确定
        if Page.element_is_exist(common, '公共页面', '一人多户同步提示弹窗'):
            Page.click(common, "公共页面", "一人多户弹框确定按钮")
            Page.logger.info("此次修改的信息将同步到您在我司开立的其他账户。--已确认", extra={"appname": "update_id_card"})